export default {
	data(){
		return{
			match: null,
			matchZsList: [],
			matchId: 0,
			tabArr: [0],
			tabCur: 0,
			leftCur: 0,
			windowHeight: 0,
			isShow: false,
		}
	},
	computed: {
		user() {
			return this.$store.state.user
		},
		zsType() {
			return this.tabList[this.tabCur].type
		},
		getColor(){//获取颜色
			return (index, type) => {
				if(this.matchZsList[this.zsType][this.leftCur].oddsDetails[index].closingFlag){
					return 'black'
				}
				if(this.matchZsList[this.zsType][this.leftCur].oddsDetails[index+1]){
					if(Number(this.matchZsList[this.zsType][this.leftCur].oddsDetails[index][type]) > Number(this.matchZsList[this.zsType][this.leftCur].oddsDetails[index+1][type])){
						return 'red'
					}else if(Number(this.matchZsList[this.zsType][this.leftCur].oddsDetails[index][type]) < Number(this.matchZsList[this.zsType][this.leftCur].oddsDetails[index+1][type])){
						return 'green'
					}else{
						return 'black'
					}
				}else{
					return 'black'
				}
			}
		},
	},
		updated() {
			console.log('updated');
			if(this.matchZsList[this.zsType] && this.matchZsList[this.zsType][this.leftCur]){
				this.$nextTick(() => {
					this.scrollPage(false)
				})
			}
		},
		onLoad() {
			console.log(this.$Route.query);
			if(this.$Route.query.mid){
				let query = this.$Route.query.mid.split('_')
				console.log(query);
				this.tabCur = query[0]
				this.leftCur = query[1]
				this.matchId = query[2]
			}
			if(this.matchId && this.sportId){
				this.getMatchInfo()
				this.getMatchZsList()
			}else{
				//非法请求处理
				uni.showToast({
					title: '请求数据错误，请返回重试',
					icon: 'none'
				})
				this.$goBack(1)
			}
		},
		onReady() {
			this.windowHeight = uni.getSystemInfoSync().windowHeight
		},
		onPageScroll(e) {
			if(!this.timeout){
				this.timeout = setTimeout(()=>{
					this.timeout = null
					this.scrollPage(false)
				}, 100)
			}
		},
		onPullDownRefresh() {
			this.getMatchInfo()
			this.getMatchZsList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1000)
		},
		methods: {
			//选项卡切换
			tabChange(index) {
				this.tabCur = index;
				this.leftCur = 0
				if(this.tabArr.indexOf(index) < 0){
					this.tabArr.push(index)
				}
				if(!this.matchZsList[this.zsType]){
					this.getMatchZsList()
				}
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
			},
			//左侧切换
			leftChange(index) {
				this.leftCur = index;
				if(!this.matchZsList[this.zsType][this.leftCur].oddsDetails){
					this.getMatchZsDetails(this.matchZsList[this.zsType][this.leftCur].companyId)
				}
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
			},
			//比赛基础信息
			getMatchInfo() {
				this.$http.get({
					url: '/api/queryMatchBaseInfo',
					data: {
						matchId: this.matchId,
						sportId: this.sportId
					},
				}).then(data => {
					console.log(data);
					if(data.success && data.result){
						this.match = data.result
					}else{
						uni.showToast({
							title: '暂无比赛信息',
							icon: 'none'
						})
					}
				}).catch((err) => {
					console.log(err);
				})
			},
			//指数信息
			getMatchZsList() {
				this.$http.get({
					url: '/api/oddsList',
					data: {
						matchId: this.matchId,
						sportId: this.sportId,
						zsType: this.zsType
					},
				}).then(data => {
					console.log(data);
					if(data.success && data.result[0]){
						this.$set(this.matchZsList, this.zsType, data.result)
						this.getMatchZsDetails(this.matchZsList[this.zsType][this.leftCur].companyId)
					}else{
						uni.showToast({
							title: '暂无指数信息',
							icon: 'none'
						})
					}
				}).catch((err) => {
					console.log(err);
				})
			},
			getMatchZsDetails(companyId) {
				this.$http.get({
					url: '/api/oddsDetails',
					data: {
						companyId,
						matchId: this.matchId,
						sportId: this.sportId,
						zsType: this.zsType
					},
				}).then(data => {
					console.log(data);
					if(data.success && data.result[0]){
						this.matchZsList[this.zsType][this.leftCur].oddsDetails = data.result
						this.$forceUpdate()
					}else{
						uni.showToast({
							title: '暂无指数信息',
							icon: 'none'
						})
					}
				}).catch((err) => {
					console.log(err);
				})
			},
			//页面滚动
			scrollPage(scroll) {
				uni.createSelectorQuery().select('.body-detail > .right').boundingClientRect(data => {
					uni.createSelectorQuery().select('#sqzs').boundingClientRect(res => {
						if(res){
							if(scroll){
								uni.pageScrollTo({
									duration: 0,
									scrollTop: res.top - data.top
								})
							}else{
								if(res.top < 100){
									this.isShow = true
								}else if(res.top > this.windowHeight){
									this.isShow = true
								}else{
									this.isShow = false
								}
							}
						}
				　　}).exec()
				}).exec()
			}
		}
}